package com.fans.oauths.server.oauth2.config.oauth;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;

import com.fans.oauths.server.oauth2.redis.service.RedisAuthorizationCodeServices;
import com.fans.oauths.server.oauth2.redis.service.RedisClientDetailsService;

/**
 * @author fans
 * @version 创建时间：2017年11月12日 上午22:57:51
 */
@Configuration

public class OAuth2ServerConfig {
    @Resource
    private DataSource dataSource;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

	/**
	 * 向容器中 注入 RedisClientDetailsService 对象
	 * @return
	 */
    @Bean 
    @ConditionalOnProperty(prefix = "security.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
    public RedisClientDetailsService clientDetailsService() {
        RedisClientDetailsService clientDetailsService = new RedisClientDetailsService(dataSource);
        clientDetailsService.setRedisTemplate(redisTemplate);
        return clientDetailsService;
    }

    /**
	 * 向容器中 注入 RandomValueAuthorizationCodeServices 对象
	 * @return
	 */
    @Bean
    public RandomValueAuthorizationCodeServices authorizationCodeServices() {
        RedisAuthorizationCodeServices redisAuthorizationCodeServices = new RedisAuthorizationCodeServices();
        redisAuthorizationCodeServices.setRedisTemplate(redisTemplate);
        return redisAuthorizationCodeServices;
    }
}
